<?php

class Azio_Posts
{

    public function __construct()
    {

    }

    public static function get_excerpt($postid, $length = 350)
    {
        $post = get_post($postid);
        //var_dump($post);
        if (!empty($post->post_excerpt)) {
            $excerpt = $post->post_excerpt;
        } else {
            // apply_filters('the_content', get_post_field('post_content', $postid));
            $excerpt = azio_cut_tring_by_char(strip_tags($post->post_content), $length);
        };
        return $excerpt;
    }



    public function getTermsByParent($term, $taxonomy = "category")
    {

        $args = array(
            'type' => 'post',
            'child_of' => $term,
            'orderby' => 'name',
            'order' => 'ASC',
        );
        $result = get_terms($taxonomy, $args);
        foreach ($result as &$value) {
            $value->permalink = get_term_link(intval($value->term_id), $taxonomy);
        }

        return $result;
    }


    public static function getPosts($limit, $category = "", $meta_key = "", $meta_value = "", $post_not_in = "", $orderby = "post_date", $order = "DESC")
    {

        $args = array(
            'posts_per_page' => $limit,
            'orderby' => $orderby,
            'order' => $order,
            'post_type' => 'post',
            'post_status' => 'publish',
        );

        if (!empty($category)) {
            $args['category'] = $category;
        }

        if (!empty($meta_key)) {
            $args['meta_key'] = $meta_key;
            $args['meta_value'] = $meta_value;
        }

        if (!empty($post_not_in)) {
            $args['post__not_in'] = $post_not_in;
        }

        $objects = get_posts($args);

        foreach ($objects as $object) {
            $object->filter = 'sample';
            $permalink = get_permalink($object);
            $object->permalink = $permalink;

            $thumbnail_id = get_post_thumbnail_id($object->ID);
            $image = wp_get_attachment_image_src($thumbnail_id, 'thumbnail', true);
            $object->image_thumbnail = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'avatar', true);
            $object->image_avatar = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'medium', true);
            $object->image_medium = $image[0];

            $image = wp_get_attachment_image_src($thumbnail_id, 'large', true);
            $object->image_large = $image[0];

            $object->excerpt = Azio_Posts::get_excerpt($object->ID);

            $result[] = $object;
        }

        return $result;
    }

    public static function getRelatedProducts($limit, $term_id, $post_not_in)
    {
        $args = array(
            'posts_per_page' => $limit,
            'orderby' => 'rand',
            'post_type' => 'product',
            'post_status' => 'publish',
            'post__not_in' => $post_not_in,
            'tax_query' => array(
                array(
                    'taxonomy' => 'producttype',
                    'field' => 'id',
                    'terms' => $term_id
                )
            )
        );


        $objects = get_posts($args);

        foreach ($objects as $object) {
            $object->filter = 'sample';
            $permalink = get_permalink($object);
            $object->permalink = $permalink;

            $thumbnail_id = get_post_thumbnail_id($object->ID);
            $image = wp_get_attachment_image_src($thumbnail_id, 'thumbnail', true);
            $object->image_thumbnail = $image[0];

            $result[] = $object;
        }

        return $result;
    }


    public static function getProductField($ID)
    {
//        $result['showindex'] = get_post_meta($ID, 'showindex', true);
//        $result['featured'] = get_post_meta($ID, 'featured', true);
//        $result['gia'] = get_post_meta($ID, 'gia', true);
//        $result['donvithanhtoan'] = get_post_meta($ID, 'donvithanhtoan', true);
//        $result['donvithanhtoan'] = get_post_meta($ID, 'donvithanhtoan', true);
//        $result['thongso'] = get_post_meta($ID, 'thongso', true);
//        $result['thongsochitiet'] = get_post_meta($ID, 'thongsochitiet', false);

        global $cfs;
        $result['showindex'] = $cfs->get('showindex', $ID);
        $result['featured'] = $cfs->get('featured', $ID);
        $result['gia'] = $cfs->get('gia', $ID);
        $result['donvithanhtoan'] = $cfs->get('donvithanhtoan', $ID);
        $result['thongso'] = $cfs->get('thongso', $ID);

        return $result;
    }

    public static function getAllProducttype()
    {
        $args = array(
            'orderby' => 'id',
            'order' => 'ASC',
            'hide_empty' => true,
        );

        $result = get_terms('producttype', $args);
        return $result;
    }

}